/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: maintabwidget.cpp 401 2009-05-10 09:58:00Z jpnurmi $
*/

#include "maintabwidget.h"
#include <QApplication>
#include <QShortcut>
#include <QTabBar>
#include <QStyle>
#include <QTimer>

MainTabWidget::MainTabWidget(QWidget* parent) :
    QTabWidget(parent)
{
    // workaround QTextEdit::append() autoscrolling:
    // tab icons are too big by default -> the tab bar gets resized when icons appear
    // -> the text edit gets resized and autoscroll is messed up
    int s = style()->pixelMetric(QStyle::PM_TabBarIconSize, 0, this) - 2;
    setIconSize(QSize(s, s));

    setTabPosition(QTabWidget::West);
    setStyleSheet(".MainTabWidget::pane { border: 0px; }");

    QShortcut* shortcut = new QShortcut(QKeySequence(tr("Alt+Up")), this);
    connect(shortcut, SIGNAL(activated()), this, SLOT(moveToPrevTab()));

    shortcut = new QShortcut(QKeySequence(tr("Alt+Down")), this);
    connect(shortcut, SIGNAL(activated()), this, SLOT(moveToNextTab()));

    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabActivated(int)));
}

void MainTabWidget::tabInserted(int index)
{
    if (widget(index)->inherits("SessionTabWidget"))
    {
        connect(widget(index), SIGNAL(tabIconChanged(QIcon)), this, SLOT(setTabIcon(QIcon)));
        connect(widget(index), SIGNAL(tabTextChanged(QString)), this, SLOT(setTabText(QString)));
    }

    tabBar()->setVisible(count() > 1);
}

void MainTabWidget::tabRemoved(int index)
{
    Q_UNUSED(index);
    tabBar()->setVisible(count() > 1);
}

void MainTabWidget::moveToNextTab()
{
    int index = currentIndex();
    if (++index >= count())
        index = 0;
    setCurrentIndex(index);
}

void MainTabWidget::moveToPrevTab()
{
    int index = currentIndex();
    if (--index < 0)
        index = count() - 1;
    setCurrentIndex(index);
}

void MainTabWidget::tabActivated(int index)
{
    Q_UNUSED(index);

    if (!currentWidget())
    {
        window()->setWindowTitle(QApplication::applicationName());
        return;
    }
    
    if (currentWidget()->inherits("SessionTabWidget"))
        QTimer::singleShot(500, currentWidget(), SLOT(tabActivated()));
}

void MainTabWidget::setTabIcon(const QIcon& icon)
{
    if (!sender() || !sender()->isWidgetType())
        return;

    int index = indexOf(static_cast<QWidget*>(sender()));
    if (index != -1)
        QTabWidget::setTabIcon(index, icon);
}

void MainTabWidget::setTabText(const QString& text)
{
    if (!sender() || !sender()->isWidgetType())
        return;

    int index = indexOf(static_cast<QWidget*>(sender()));
    if (index != -1)
        QTabWidget::setTabText(index, text);
}
